package com.jiangxiaofei.citycloud.mvp;

import android.content.Context;
import android.widget.Toast;

import com.jiangxiaofei.citycloud.constants.Constants;
import com.jiangxiaofei.citycloud.httpUtil.OnLoadDataIm;
import com.jiangxiaofei.citycloud.param.VillageArticleParam;
import com.jiangxiaofei.citycloud.param.VillageListParam;

/**
 * Created by 上官林超 on 2017/7/30.
 */

public class VillagePersenter implements VillageConstant.VillageMenuPersenter, VillageConstant.VillageListPersenter,
        VillageConstant.VillageDetailPersenter, VillageConstant.VillageAddArticlePersenter, VillageConstant.VillageUpdateArticlePersenter {
    Context mContext;
    //获取美丽乡村菜单
    VillageConstant.VillageMenuModel mVillageMenuModel;
    VillageConstant.VillagemMenuView mVillagemMenuView;

    public VillagePersenter(Context mContext, VillageConstant.VillagemMenuView villagemMenuView) {
        this.mContext = mContext;
        this.mVillagemMenuView = villagemMenuView;
        this.mVillageMenuModel = new VillageModel();
    }

    //    获取乡村信息列表
    VillageConstant.VillageListModel mVillageListModel;
    VillageConstant.VillageListView mVillageListView;

    public VillagePersenter(Context mContext, VillageConstant.VillageListView mVillageListView) {
        this.mContext = mContext;
        this.mVillageListView = mVillageListView;
        this.mVillageListModel = new VillageModel();
    }

    //获取乡村信息详情页
    VillageConstant.VillageDetailModel mVillageDetailModel;
    VillageConstant.VillageDetailView mVillageDetailView;

    public VillagePersenter(Context mContext, VillageConstant.VillageDetailView mVillageDetailView) {
        this.mContext = mContext;
        this.mVillageDetailView = mVillageDetailView;
        this.mVillageDetailModel = new VillageModel();
    }

    //发布新文章
    VillageConstant.VillageAddArticleModel mVillageAddArticleModel;
    VillageConstant.VillageAddArticleView mVillageAddArticleView;

    public VillagePersenter(Context mContext, VillageConstant.VillageAddArticleView mVillageAddArticleView) {
        this.mContext = mContext;
        this.mVillageAddArticleView = mVillageAddArticleView;
        this.mVillageAddArticleModel = new VillageModel();
    }

    //更新文章
    VillageConstant.VillageUpdateArticleModel mVillageUpdateArticleModel;
    VillageConstant.VillageUpdateArticleView mVillageUpdateArticleView;

    public VillagePersenter(Context mContext, VillageConstant.VillageUpdateArticleView mVillageUpdateArticleView) {
        this.mContext = mContext;
        this.mVillageUpdateArticleView = mVillageUpdateArticleView;
        this.mVillageUpdateArticleModel = new VillageModel();
    }

    /**
     * 获取美丽乡村菜单
     */
    @Override
    public void getVillageMenu() {
        mVillageMenuModel.getVillageMenu(mContext, new OnLoadDataIm() {
            @Override
            public void onSuccess(Object o, int resultCode) {
                if (o != null) {
                    mVillagemMenuView.setData(o);
                }
            }

            @Override
            public void onSuccess(Object o, String message, int resultCode) {

            }

            @Override
            public void onFailure(String msg, int resultCode) {

            }
        });
    }

    //    文章详情
    @Override
    public void getVillageList() {
        mVillageListView.showLoading();
        VillageListParam villageListParam = mVillageListView.getVillageList();
        if (villageListParam != null) {
            mVillageListModel.getVillageList(mContext, villageListParam.getPage(), villageListParam.getCategory(), new OnLoadDataIm() {
                @Override
                public void onSuccess(Object o, int resultCode) {

                }

                @Override
                public void onSuccess(Object o, String message, int resultCode) {
                    mVillageListView.setData(o);
                    mVillageListView.hideLoading();
                }

                @Override
                public void onFailure(String msg, int resultCode) {
                    Toast.makeText(mContext, "没有更多数据了", Toast.LENGTH_SHORT).show();
                    mVillageListView.hideLoading();

                }
            });
        }
    }

    /**
     * 获取文章详情页
     */
    @Override
    public void getVillageDetail() {
        mVillageDetailView.showLoading();
        int id = mVillageDetailView.getVillageDetail();
        if (id != 0) {
            mVillageDetailModel.getVillageDetail(mContext, id, new OnLoadDataIm() {
                @Override
                public void onSuccess(Object o, int resultCode) {

                }

                @Override
                public void onSuccess(Object o, String message, int resultCode) {
                    mVillageDetailView.setData(o);
                    mVillageDetailView.hideLoading();
                }

                @Override
                public void onFailure(String msg, int resultCode) {

                }
            });
        }
    }

    /**
     * 发布文章
     */
    @Override
    public void addVillageArticle() {
        VillageArticleParam villageArticleParam = mVillageAddArticleView.addVillageArticle();
        if (villageArticleParam != null) {
            mVillageAddArticleModel.addVillageArticle(mContext, villageArticleParam.getTitle(), villageArticleParam.getContent(),
                    villageArticleParam.getAuthor(), villageArticleParam.getCategory(), villageArticleParam.getImg(),
                    villageArticleParam.getLocation(), new OnLoadDataIm() {
                        @Override
                        public void onSuccess(Object o, int resultCode) {

                        }

                        @Override
                        public void onSuccess(Object o, String message, int resultCode) {
                            if (resultCode == 1) {
                                mVillageAddArticleView.setData(Constants.AddSuccess);
                                Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onFailure(String msg, int resultCode) {

                        }
                    });
        }
    }

    /**
     * 修改文章
     */
    @Override
    public void updateVillageArticle() {
        mVillageUpdateArticleView.showLoading();
        VillageArticleParam villageArticleParam = mVillageUpdateArticleView.updateVillageArticle();
        if (villageArticleParam != null) {
            mVillageUpdateArticleModel.updateVillageArticle(mContext, villageArticleParam.getTitle(), villageArticleParam.getContent(),
                    villageArticleParam.getAuthor(), villageArticleParam.getCategory(), villageArticleParam.getImg(),
                    villageArticleParam.getLocation(), villageArticleParam.getId(), new OnLoadDataIm() {
                        @Override
                        public void onSuccess(Object o, int resultCode) {

                        }

                        @Override
                        public void onSuccess(Object o, String message, int resultCode) {
                            if (resultCode == 1) {
                                mVillageUpdateArticleView.setData(Constants.UpdateSuccess);
                                mVillageUpdateArticleView.hideLoading();
                            }
                        }

                        @Override
                        public void onFailure(String msg, int resultCode) {

                        }
                    });
        }
    }
}
